package com.seeyon.apps.restext;

import java.util.HashMap;

import java.util.Map;
import java.io.File;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import com.sun.corba.se.impl.encoding.OSFCodeSetRegistry.Entry;

import java.util.List;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import com.seeyon.client.CTPRestClient;

public class RestFormDemorestsample111  {

//导出无流程表单数据接口(Since:56)
public static void testExportBusinessFormData() {
   CTPRestClient client = ClientResource.getInstance().resouresClent();
   String result = client.get("form/export/123456?beginDateTime=2021-06-06 00:00:59&endDateTime=2021-09-09 00:00:59", String.class);
   System.out.println(result);
}

//发起无流程表单接口实现(Since:56)
public static void testImportBusinessFormData() {
   
   Map res = new HashMap();
   res.put("loginName", "zjl");//loginName:OA人员登录名(String型)
   res.put("dataXml", null);//dataXml：无流程表单XML信息(String型)。请参考BPM生成的XML格式，与有流程XML差别只在于 无流程<forms version="2.1"> ，而有流程<forms version="2.0">，其他都一样
   
   try {
	    SAXBuilder builder = new SAXBuilder();
	    Document doc = builder.build(new File("src/com/seeyon/apps/restext/exportMatetialData(1).xml"));
	    Element foo = doc.getRootElement();
	    List<?> allChildren = foo.getChildren();
	    
//	    System.out.println("    :"+ ( allChildren.size()));
	   for (int i = 0; i < allChildren.size(); i++) {
		   String XmlFile = null;
		   if (i%2 == 0) {
			
		
		    XmlFile = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
		   		+ "<forms version=\"2.1\">\n"+"  <formExport>\n"
		   		+ "    <summary id=\"248113986419574543\" name=\"formmain_0051\"/>\n"
		   		+ "    <definitions>\n"
+ "      <column id=\"field0001\" type=\"0\" name=\"辅助装置\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0002\" type=\"0\" name=\"物料商标\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0003\" type=\"0\" name=\"长度单位\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0004\" type=\"0\" name=\"序列单元\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0005\" type=\"0\" name=\"图片计数\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0006\" type=\"0\" name=\"加权\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0007\" type=\"2\" name=\"建立时间\" isNullable=\"false\" length=\"255\"/>\n"
+ "      <column id=\"field0008\" type=\"0\" name=\"模型\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0009\" type=\"0\" name=\"注册\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0010\" type=\"0\" name=\"保证书编号\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0011\" type=\"0\" name=\"物料名称ID\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0012\" type=\"2\" name=\"最后更新时间\" isNullable=\"false\" length=\"255\"/>\n"
+ "      <column id=\"field0013\" type=\"0\" name=\"日最低需要量\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0014\" type=\"0\" name=\"创建者\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0015\" type=\"0\" name=\"版本\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0016\" type=\"0\" name=\"基本计量单位\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0017\" type=\"0\" name=\"使用状态关系\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0018\" type=\"0\" name=\"不同步\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0077\" type=\"0\" name=\"冷冻装置\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0078\" type=\"0\" name=\"材料组\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0079\" type=\"0\" name=\"描述\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0080\" type=\"0\" name=\"设备属性\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0081\" type=\"0\" name=\"数据ID\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0082\" type=\"0\" name=\"宽度\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0083\" type=\"0\" name=\"材料组第二层\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0084\" type=\"0\" name=\"总宽度\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0085\" type=\"0\" name=\"物料英文名\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0086\" type=\"0\" name=\"物料名称简称\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0088\" type=\"0\" name=\"高度\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0089\" type=\"0\" name=\"有效状态\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0090\" type=\"0\" name=\"上次安装日期用户\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0091\" type=\"0\" name=\"外包\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0092\" type=\"0\" name=\"价格精度\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0094\" type=\"0\" name=\"状态\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0096\" type=\"0\" name=\"简称\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0099\" type=\"0\" name=\"物料名称简拼\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0100\" type=\"0\" name=\"物料名称\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0101\" type=\"0\" name=\"长度\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0102\" type=\"0\" name=\"控制单元\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0103\" type=\"0\" name=\"净重\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0104\" type=\"0\" name=\"体积\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0105\" type=\"0\" name=\"长号码\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0106\" type=\"0\" name=\"材料\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0107\" type=\"0\" name=\"材料组标准\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0108\" type=\"0\" name=\"代码提示\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0109\" type=\"0\" name=\"重量\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0110\" type=\"0\" name=\"旧号码\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0111\" type=\"0\" name=\"助理\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0118\" type=\"0\" name=\"查询\" isNullable=\"false\" length=\"20\"/>\n"
+ "      <column id=\"field0119\" type=\"0\" name=\"条形码\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0037\" type=\"0\" name=\"编号\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0120\" type=\"0\" name=\"货物\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0042\" type=\"0\" name=\"别名\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0121\" type=\"0\" name=\"物料名称拼音\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0122\" type=\"0\" name=\"管理员\" isNullable=\"false\" length=\"100\"/>\n"
+ "      <column id=\"field0124\" type=\"0\" name=\"体积单位\" isNullable=\"false\" length=\"100\"/>"
		   		+ "    </definitions>\n"
		   		+ "    <values>\n"
		   		+ "      <column name=\"辅助装置\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("assistUnit").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"物料商标\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("materialTrademark").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"长度单位\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("lengthUnit").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"序列单元\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("seqUnit").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"图片计数\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("pictureNumber").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"加权\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("isWeighted").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"建立时间\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("createTime").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"模型\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("model").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"注册\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("registeredmark").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"保证书编号\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("warrantnumber").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"物料名称ID\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("name4idx").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"最后更新时间\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("lastUpdateTime").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"日最低需要量\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("isToMDR").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"创建者\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("creator").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"版本\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("version").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"基本计量单位\">\n"
		   		+ "        <value>"+ ((Element) allChildren.get(i)).getChild("baseUnit").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"使用状态关系\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("useAsstAttrRelation").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"不同步\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("isSynochronous").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"冷冻装置\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("freezeOrgUnit").getText()+"<![CDATA[1]]></value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"材料组\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("materialGroup").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"描述\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("description").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"设备属性\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("equipProperty").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"数据ID\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("number4idx").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"宽度\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("width").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"材料组第二层\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i+1)).getChild("entry").getChild("materialGroup").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"总宽度\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("grossWeight").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"物料英文名\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("foreignname").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"物料名称简称\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("simpleName").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"高度\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("height").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"有效状态\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("effectedStatus").getText()+"<![CDATA[1]]></value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"上次安装日期用户\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("lastUpdateUser").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"外包\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("isOutsourcedPart").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"价格精度\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("pricePrecision").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"状态\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("status").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"简称\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("shortName").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"物料名称简拼\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("simpleName").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"物料名称\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("name").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"长度\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("length").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"控制单元\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("CU").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"净重\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("netWeight").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"体积\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("volume").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"长号码\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("longNumber").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"材料\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i+1)).getChild("entry").getChild("material").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"材料组标准\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i+1)).getChild("entry").getChild("materialGroupStandard").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"代码提示\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("helpCode").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"重量\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("weightUnit").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"旧号码\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("oldNumber").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"助理\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("assistAttr").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"查询\">\n"
		   		+ "        <value></value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"条形码\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("barCode").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"编号\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("number").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"货物\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("isGoods").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"别名\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("alias").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"物料名称拼音\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("namePinYin").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"管理员\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("adminCU").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "      <column name=\"体积单位\">\n"
		   		+ "        <value>"+((Element) allChildren.get(i)).getChild("volumnUnit").getText()+"</value>\n"
		   		+ "      </column>\n"
		   		+ "    </values>\n"
		   		+ "    <subForms/>\n"	   		
		   		+ "  </formExport>\n"
		   		+ "</forms>"; 
		   }	   
		   
		   else {
			continue;
		}
		   
//	    System.out.println(XmlFile);
		  
	   	
//   res.put("loginName", "zjl");//loginName:OA人员登录名(String型)
   res.replace("dataXml", XmlFile);//dataXml：无流程表单XML信息(String型)。请参考BPM生成的XML格式，与有流程XML差别只在于 无流程<forms version="2.1"> ，而有流程<forms version="2.0">，其他都一样
	   
   	
   CTPRestClient client = ClientResource.getInstance().resouresClent();
   String result = client.post("form/import/123456", res, String.class);

   System.out.println(result);
}}
   catch (Exception e) {
	    e.printStackTrace();
	   }   
}

//更新无流程表单数据接口实现(Since:V6.0SP1)
/**dataXml示例
<?xml version="1.0" encoding="utf-8"?>
<forms version="2.1">
	<formExport>
	<summary id="-6081324203087609682" name="formmain_0031"/>
	<definitions></definitions>
	<values>
		<column name="field3">
			<value><![CDATA[updateValue]]></value>
		</column>
	</values>
	<subForms>
		<subForm>
			<definitions>
				<column id="field0003" type="0" name="field1" length="255"/>
				<column id="field0004" type="0" name="field2" length="255"/>
			</definitions>
			<values>
				<row>
					<column name="field1">
						<value><![CDATA[updateValue1]]></value>
						<title><![CDATA[updateValue2]]></title>  <!-title填写值为 formson_{重表}的ID->
					</column>
					<column name="field2">
						<value><![CDATA[updateValue3]]></value>
						<title><![CDATA[updateValue4]]></title>
					</column>
				</row>
			</values>
		</subForm>
</subForms>
</formExport>
</forms>
**/
public static void testupdateBusinessFormData() {
   Map res = new HashMap();
   res.put("templateCode", "123456");//无流程表单模板编号(String型)
   res.put("moduleId", "1368693008068595023");//主表ID(String型)
   res.put("loginName", "zjl");//OA人员登录名(String型)
   res.put("dataXml", "start_member_id = "6301610059689640710"");//只需要填写更新字段XML信息(String型)，请参见上面示例

   CTPRestClient client = ClientResource.getInstance().resouresClent();
   String result = client.put("form/update", res, String.class);
   System.out.println(result);
}

public static void main(String args[]){
// 	testExportBusinessFormData();
    testImportBusinessFormData();
//   testupdateBusinessFormData();



}
}